package com.zhku.shoppingspringboot.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhku.shoppingspringboot.common.R;
import com.zhku.shoppingspringboot.pojo.dto.AllSelectedDTO;
import com.zhku.shoppingspringboot.pojo.dto.MergeCartDTO;
import com.zhku.shoppingspringboot.pojo.entity.ShopCart;
import com.zhku.shoppingspringboot.pojo.vo.ShowShopCartsVO;
import com.zhku.shoppingspringboot.service.IShopCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zhku.shoppingspringboot.common.ResultCode.ERROR;

/**
 * @author hwy
 * @date 2024-12-25 22:42
 * 购物车模块
 */
@RestController
@RequestMapping("/member/cart")
@Slf4j
@Api(tags = "购物车管理")
public class ShopCartController {
    @Autowired
    private IShopCartService iShopCartService;

    /**
     * 合并购物车
     */
    @ApiOperation("合并购物车")
    @PostMapping("/merge")
    public R<String> mergeCart(
            @RequestBody
            @Parameter(description = "合并购物车信息")
            List<MergeCartDTO> mergeCartDTOs
    ){
        return iShopCartService.mergeCart(mergeCartDTOs);
    }

    /**
     * 展示购物车列表
     */
    @ApiOperation("展示购物车列表")
    @GetMapping()
    public R<List<ShowShopCartsVO>> showShopCart(){
        List<ShowShopCartsVO> showShopCartsVOS = iShopCartService.showShopCart();
        return R.success(showShopCartsVOS);
    }

    /**
     * 加入购物车
     */
    @ApiOperation("加入购物车")
    @PostMapping()
    public R<ShowShopCartsVO> addShopCart(
            @RequestBody
            MergeCartDTO mergeCartDTOs
    ){
        ShowShopCartsVO showShopCartsVOS = iShopCartService.addShopCart(mergeCartDTOs);
        if(ObjectUtil.isEmpty(showShopCartsVOS)){
            return R.error(ERROR,"加入失败");
        }
        return R.success(showShopCartsVOS);
    }

    /**
     * 删除购物车商品
     */
    @ApiOperation("删除购物车商品")
    @DeleteMapping()
    public R<String> deleteShopCart(
            @RequestParam("ids")
            @Parameter(description = "要删除的购物车里商品的skuId集合",required = true)
            List<Integer> ids
    ){
        return iShopCartService.deleteShopCart(ids);
    }

    /**
     * 修改商品
     */
    @ApiOperation("修改-购物车商品")
    @PutMapping("/{id}")
    public R<ShowShopCartsVO> modifyGood(
            @PathVariable("id")
            @Parameter(description = "商品sku的id",required = true)
            String id,
            @RequestBody
            MergeCartDTO mergeCartDTOs
    ){

        ShowShopCartsVO showShopCartsVO = iShopCartService.modifyGood(Long.parseLong(id), mergeCartDTOs);
        if(showShopCartsVO == null){
            return R.error(ERROR,"修改失败");
        }
        return R.success(showShopCartsVO);
    }

    /**
     * 修改购物车商品状态 全选或取消全选
     */
    @ApiOperation("修改购物车商品状态 全选或取消全选")
    @PutMapping("/selected")
    public R<String> AllSelected(
            @RequestBody
            AllSelectedDTO allSelectedDTO
    ){
        return iShopCartService.AllSelected(allSelectedDTO);
    }
}
